package com.elai.overview.service.impl;

import java.util.List;

import com.elai.common.core.domain.model.LoginUser;
import com.elai.common.utils.DateUtils;
import com.elai.common.utils.SecurityUtils;
import com.elai.overview.domain.MyOverviewPage;
import com.elai.overview.service.IMyOverviewPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elai.overview.mapper.MyOverviewTeamMapper;
import com.elai.overview.domain.MyOverviewTeam;
import com.elai.overview.service.IMyOverviewTeamService;

/**
 * 我的概览Service业务层处理
 * 
 * @author elai
 * @date 2021-08-29
 */
@Service
public class MyOverviewTeamServiceImpl implements IMyOverviewTeamService 
{
    @Autowired
    private MyOverviewTeamMapper myOverviewTeamMapper;

    @Autowired
    private IMyOverviewPageService myOverviewPageService;

    /**
     * 查询我的概览
     * 
     * @param id 我的概览ID
     * @return 我的概览
     */
    @Override
    public MyOverviewTeam selectMyOverviewTeamById(Long id)
    {
        return myOverviewTeamMapper.selectMyOverviewTeamById(id);
    }

    /**
     * 查询我的概览列表
     * 
     * @param myOverviewTeam 我的概览
     * @return 我的概览
     */
    @Override
    public List<MyOverviewTeam> selectMyOverviewTeamList(MyOverviewTeam myOverviewTeam)
    {
        return myOverviewTeamMapper.selectMyOverviewTeamList(myOverviewTeam);
    }

    /**
     * 新增我的概览
     * 
     * @param myOverviewTeam 我的概览
     * @return 结果
     */
    @Override
    public int insertMyOverviewTeam(MyOverviewTeam myOverviewTeam)
    {
        LoginUser user= SecurityUtils.getLoginUser();
        myOverviewTeam.setUserId(user.getUser().getUserId());
        myOverviewTeam.setCreateBy(user.getUsername());
        myOverviewTeam.setCreateTime(DateUtils.getNowDate());
        return myOverviewTeamMapper.insertMyOverviewTeam(myOverviewTeam);
    }

    /**
     * 修改我的概览
     * 
     * @param myOverviewTeam 我的概览
     * @return 结果
     */
    @Override
    public int updateMyOverviewTeam(MyOverviewTeam myOverviewTeam)
    {
        return myOverviewTeamMapper.updateMyOverviewTeam(myOverviewTeam);
    }

    /**
     * 批量删除我的概览
     * 
     * @param ids 需要删除的我的概览ID
     * @return 结果
     */
    @Override
    public int deleteMyOverviewTeamByIds(Long[] ids)
    {
        return myOverviewTeamMapper.deleteMyOverviewTeamByIds(ids);
    }

    /**
     * 删除我的概览信息
     * 
     * @param id 我的概览ID
     * @return 结果
     */
    @Override
    public int deleteMyOverviewTeamById(Long id)
    {
        return myOverviewTeamMapper.deleteMyOverviewTeamById(id);
    }

    @Override
    public int setMyOverviewTeam(List<MyOverviewTeam> teamList) {
       for (int i=0;i<teamList.size();i++){
           MyOverviewTeam team=teamList.get(i);
           team.setSortNo(Long.valueOf(i+1));
           myOverviewTeamMapper.updateMyOverviewTeam(team);
           List<MyOverviewPage>  pages=team.getPageList();
           for(int j=0;j<pages.size();j++){
               MyOverviewPage page=pages.get(j);
               MyOverviewPage newPage=new MyOverviewPage();
               newPage.setId(page.getId());
               newPage.setSortNo(Long.valueOf(j+1));
               newPage.setTeamId(team.getId());
               myOverviewPageService.updateMyOverviewPage(newPage);
           }
       }
        return 1;
    }
}
